//
//  NSString+Ybr.m
//  wifibox
//
//  Created by os X 10.9 on 15-1-15.
//  Copyright (c) 2015年 yinbaner. All rights reserved.
//

#import "NSString+Ybr.h"

@implementation NSString (Ybr)
+(BOOL) isEmpty:(NSString *) content {
    if([content isKindOfClass:[NSNull class]] || content == nil || content.length == 0) {
        return YES;
    }
    return NO;
}

+(NSString *) UTFToGB2312:(NSString *) utf8Str {
    NSStringEncoding enCode = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF8);
    NSData *gb2312 = [utf8Str dataUsingEncoding:enCode];
    return [[NSString alloc] initWithData:gb2312 encoding:NSUTF8StringEncoding];
}

+(int) MyHashCode:(NSString *) str {
    if([NSString isEmpty:str]) {
        return 0;
    }
    
    NSStringEncoding ens = CFStringConvertEncodingToNSStringEncoding(NSUTF16BigEndianStringEncoding);
    NSData *data = [str dataUsingEncoding:ens];
    Byte *byte = (Byte *) [data bytes];
    
    int h = 0;
    int len = (int)[data length];
    
    for (int i = 0; i < len / 2; i++) {
        int j = (((byte[i * 2] & 0xFF) << 8) | (byte[i * 2 + 1] & 0xFF));
        h = 31 * h + j;
    }
    return h;
}

-(id)JSONValue;
{
    NSData* data = [self dataUsingEncoding:NSUTF8StringEncoding];
    __autoreleasing NSError* error = nil;
    id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    if (error != nil) return nil;
    return result;
}

- (CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize
{
    NSDictionary *attrs = @{NSFontAttributeName : font};
    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}

@end
